package de.fhhof.XMLParser;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.management.modelmbean.XMLParseException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;



public class Parser extends DefaultHandler{

	
	
	public final int NOSTATE = 0;
	public final int BOOK = 1;
	public final int TITLE = 2;
	public final int AUTHOR = 3;
	public final int YEAR = 4;
	public final int PRICE = 5;
	Bookstore bookStore  = new Bookstore();
	Book currentBook = null;
	int state = 0;
	
	public int getState() {
		return state;
	}


	public void setState(int state) {
		this.state = state;
	}


	@Override
	public void characters(char[] string, int arg1, int arg2) throws SAXException {
		String s = new String(string,arg1,arg2);
		
		switch (state) {
				
		case TITLE:
			currentBook.getTitle().setText(s);
			break;
		case AUTHOR:
			currentBook.getAuthor().add(s);
			break;
		case YEAR:
			currentBook.setYear(new Integer(s));
			break;
		case PRICE:
			currentBook.setPrice(new Double(s));
			break;
			
		
		default:
			break;
		}
		
	}

	
	@Override
	public void endElement(String arg0, String arg1, String qName)
			throws SAXException {
		
		String tag = qName.toLowerCase();
		if(tag.equals("book") && currentBook != null) {
			bookStore.addBooks(currentBook);
		}
		
		setState(NOSTATE);
		
	}

	@Override
	public void startElement(String arg0, String arg1, String qName,
			Attributes atts) throws SAXException {
		
		String tag = qName.toLowerCase();
		if (tag.equals("book")){
			currentBook = new Book(atts.getValue("category"));
			
			setState(BOOK);
			
		} else if (tag.equals("title")){
			currentBook.setTitle(new Title(atts.getValue("lang")));
			setState(TITLE);
			
		}else if (tag.equals("author")){
			setState(AUTHOR);
			
		}else if(tag.equals("year")){
			setState(YEAR);
		}else if (tag.equals("price")){
			setState(PRICE);
		
		}else if (tag.equals("bookstore")){
			setState(NOSTATE);
		}
		
	}
	
	

	@Override
	public String toString() {
		return "Parser [bookStore=" +"\n" + bookStore + "]";
	}


	public static Parser parse(File in) throws Exception{
		
		Parser p1 = new Parser();
		XMLReader reader= XMLReaderFactory.createXMLReader();
		reader.setContentHandler(p1);
		reader.parse(new InputSource(new FileInputStream(in)));
		
		return p1;
		
		
		
	}
	
	public static Bookstore JDOMParser(String in) throws Exception{
		Document doc = new SAXBuilder().build(in);
		return new Bookstore(doc);
		
		
	}
	
	public static void main(String[] args) throws Exception {
		
		Parser p = Parser.parse(new File("books.xml"));
		System.out.println(p.toString());
		
		System.out.println("----------------------------------------------- \r\n");
//		XMLOutputter o = new XMLOutputter();
//		o.output(doc, System.out);
		
		
		Bookstore b = JDOMParser("books.xml");
		System.out.println(b);
		
	}
	

}
